package com.jhd.fuzhou.view.special;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.vo.IllegalRecordDtlVo;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import static com.jhd.fuzhou.view.login.MainActivity.supervisionInspectionVo;

public class SpecialIllegalBehaviorActivity extends BaseActivity {

    @BindView(R.id.btn_add_illegal)
    Button btn_add_illegal;
    @BindView(R.id.rv_illegal)
    RecyclerView rv_illegal;
    private List<IllegalRecordDtlVo> illegalRecordDtlVoVoList;
    private IllegalAdapter illegalAdapter;
    private Context context=this;
    private LinearLayoutManager layoutManager;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_illegal_behavior);
    }

    @OnClick({R.id.btn_add_illegal})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_add_illegal:
                startActivity(new Intent(context, SpecialCheckContentActivity.class));
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        refresh();
    }
    public void refresh(){
        illegalRecordDtlVoVoList = supervisionInspectionVo.getIllegalRecordDtlVoList();
        if(illegalRecordDtlVoVoList !=null){
            illegalAdapter=new IllegalAdapter(illegalRecordDtlVoVoList);
            rv_illegal.setAdapter(illegalAdapter);
        }
    }

    @Override
    protected void processLogic() {
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        layoutManager = new LinearLayoutManager(context);
        rv_illegal.setLayoutManager(layoutManager);
        refresh();
    }

    public class IllegalAdapter extends RecyclerView.Adapter<IllegalAdapter.ViewHolder> {
        List<IllegalRecordDtlVo> illegalRecordDtlVoVoList;

        public IllegalAdapter(List<IllegalRecordDtlVo> illegalRecordDtlVoVoList) {
            this.illegalRecordDtlVoVoList = illegalRecordDtlVoVoList;
        }

        @NonNull
        @Override
        public IllegalAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_illegal_behavior, parent, false);
            IllegalAdapter.ViewHolder viewHolder = new IllegalAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull IllegalAdapter.ViewHolder holder, int position) {
            //holder.rv_illegal_title.setText();
            holder.tv_delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    illegalRecordDtlVoVoList.remove(position);
                    illegalAdapter.notifyDataSetChanged();
                }
            });
            holder.rv_illegal_title.setText("违法行为("+(position+1)+")");
            holder.tv_illegal_content.setText(illegalRecordDtlVoVoList.get(position).getContent());

        }

        @Override
        public int getItemCount() {
            return illegalRecordDtlVoVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView rv_illegal_title,tv_delete,tv_illegal_content;
            public ViewHolder(View itemView) {
                super(itemView);
                this.rv_illegal_title = itemView.findViewById(R.id.rv_illegal_title);
                this.tv_delete = itemView.findViewById(R.id.tv_delete);
                this.tv_illegal_content = itemView.findViewById(R.id.tv_illegal_content);
            }
        }
    }
}